package org.example.maven.controller;

import jakarta.transaction.Transactional;
import org.example.maven.annotation.ReadOnlyMode;
import org.example.maven.annotation.logAnnotation;
import org.example.maven.pojo.Result;
import org.example.maven.pojo.role.AddRole;
import org.example.maven.pojo.role.SearchRole;
import org.example.maven.pojo.utils.Ids;
import org.example.maven.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping
    @PreAuthorize("permitAll()")
    //获取角色的listTable信息
    public Result getAllRoleListTable(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize){
        Result result=Result.success();
        result.put("data",roleService.getAllRoleList(pageNum,pageSize));
        result.put("total",roleService.getRoleCount(new SearchRole()));
        return Result.success(result);
    }

    @PostMapping
    @Transactional
    @ReadOnlyMode
    @PreAuthorize("hasAnyAuthority('role:manage')")
    @logAnnotation(
            type = "新增",
            detail = "新增角色{#addRole.roleName}"
    )
    //添加角色
    public Result addRole(@RequestBody AddRole addRole){
        String name=roleService.getRoleName((addRole.getRoleName()));
        if(name==null){
            roleService.addRole(addRole);
            roleService.addRolePermission(addRole.getId(),addRole.getPermission());
            return Result.success("添加成功");
        }else {
            return Result.error("添加的角色已存在");
        }
    }

    //查询特定角色的listTable信息
    @GetMapping("/getSpecieRole")
    @PreAuthorize("permitAll()")
    public Result getSpecieRole(SearchRole searchRole,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        Result result=Result.success();
        result.put("data",roleService.getSpecieRole(searchRole,pageNum,pageSize));
        result.put("total",roleService.getRoleCount(searchRole));
        return Result.success(result);
    }

    //根据id获取角色编辑信息
    @GetMapping("/getEditRole")
    @PreAuthorize("hasAnyAuthority('role:manage')")
    public Result getEDitRole(@RequestParam("id") Integer id){
        AddRole addRole=roleService.getEditRole(id);
        addRole.setPermission(roleService.getRolePermission(id));
        return Result.success(addRole);
    }

    //删除角色
    @PostMapping("/delete")
    @ReadOnlyMode
    @PreAuthorize("hasAnyAuthority('role:manage')")
    @logAnnotation(
            type = "删除",
            detail = "删除角色"
    )
    public Result deleteRoleById(@RequestBody Ids ids){
        roleService.deleteRole(ids.getId());
        return Result.success("删除成功");
    }

    //更新岗位信息
    @PutMapping
    @Transactional
    @ReadOnlyMode
    @PreAuthorize("hasAnyAuthority('role:manage')")
    @logAnnotation(
            type = "更新",
            detail = "更新角色{#addRole.roleName}"
    )
    public Result updateRoleById(@RequestBody AddRole addRole){
        roleService.updateRoleById(addRole);
        roleService.deleteRolePermission(Integer.valueOf(addRole.getId()));
        roleService.addRolePermission(addRole.getId(),addRole.getPermission());
        return Result.success("修改成功");
    }

    //获取角色的权限
    @GetMapping("/getRolePermission")
    @PreAuthorize("hasAnyAuthority('role:manage')")
    public Result getRolePermission(@RequestParam("id") Integer id){
        return Result.success(roleService.getRolePermission(id));
    }

    //获取角色单选框的选项
    @GetMapping("/getRoleSelect")
    @PreAuthorize("permitAll()")
    public Result getRoleSelect(){
        return Result.success(roleService.getRoleSelect());
    }
}
